package model.code.line;

import event.TextReplacementEvent;
import util.ASUtil;

import java.util.ArrayList;

/**
 * This class is for {@link Line}s that aren't full lines of code or aren't in any particular language, but is here for rendering with {@link editor.code.LineRenderer}.
 * e.g. autocomplete options, variable names/types/values, etc.
 *
 * @author Neil Dickson
 */
public class SimpleLine extends Line {
	public static final SimpleLine DUMMY = new SimpleLine(null,EMPTY_CHAR_ARRAY);

	/**
	 * This creates a SimpleLine with no associated {@link LineList}.
	 * @param text the {@link Token}s representing this line
	 */
	public SimpleLine(char[] text) {
		this(null,text);
	}

	/**
	 * This creates a SimpleLine with an associated {@link LineList}.
	 * @param lines the {@link LineList} associated with this line
	 * @param text the text of this line
	 */
	public SimpleLine(LineList lines,char[] text) {
		super(lines,text);
	}

	/**
	 * This creates a SimpleLine from a list of {@link Token}s.
	 * This is mostly a hold-over from when Tokens were used in {@link Line}s.
	 * @param tokens the list of Tokens from which to create the line
	 */
	public SimpleLine(ArrayList<Token> tokens) {
		this(EMPTY_CHAR_ARRAY);
		ensureRefCapacity(tokens.size());
		for (Token token : tokens) {
			addReference(token.getObject(),length,false);
			insert(length,token.getText());
		}
		trimToSize();
	}

	/**
	 * This just replaces the text, and not much else.
	 * @param startCol starting column (inclusive) to be replaced
	 * @param endCol ending column (exclusive) to be replaced
	 * @param newText the text to be inserted over the replaced text
	 * @return never, because this function throws NotImplementedException
	 */
	public TextReplacementEvent replace(int startCol, int endCol, String newText) {
		int lineIndex = (list!=null) ? list.indexOf(this) : -1;
		TextReplacementEvent event = new TextReplacementEvent(list,new String(text,startCol,endCol-startCol),lineIndex,startCol,lineIndex,startCol+newText.length());
		if (endCol<startCol) {
			throw new ArrayIndexOutOfBoundsException(endCol-startCol);
		}
		replaceJustText(startCol,endCol,newText.toCharArray());
		return event;
	}

	public SimpleLine copy(LineList list, int index) {
		return new SimpleLine(list, ASUtil.subCharArray(text,0,length));
	}

	public SimpleLine getDummyLine() {
		return DUMMY;
	}
}
